﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;
using Stock.Core;
using Newtonsoft.Json;
using System.ComponentModel;
using Stock.Core.Common;

namespace Web.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class WebApiTrackerAttribute : ActionFilterAttribute
    {
        private readonly string Key = "_thisWebApiOnActionMonitorLog_";

        private LogModel MonLog = new LogModel();
        private ILoggerService _logger;

        public WebApiTrackerAttribute(ILoggerService loggerFactory)
        {
            _logger = loggerFactory;
        }
        /// <summary>
        /// OnActionExecuting
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            string length = string.Empty;
            try
            {
                base.OnActionExecuting(actionContext);
                if (actionContext.Controller.GetType().FullName == "Web.RestApi.FileUpLoaderController")
                {
                    return;
                }
                
                MonLog.ExecuteStartTime = DateTime.Now;
                //获取Action 参数
                MonLog.setParams(actionContext.ActionArguments);
                MonLog.HttpRequestHeaders = JsonConvert.SerializeObject(actionContext.HttpContext.Request.Headers);
                MonLog.HttpMethod = actionContext.HttpContext.Request.Method;
                MonLog.ActionName = actionContext.ActionDescriptor.DisplayName;

                MonLog.ContentLength = actionContext.HttpContext.Request.ContentLength.ToString();

            }
            catch (Exception ex)
            {
                MonLog.Message = ex.Message;
                MonLog.StackTrace = ex.StackTrace;
                _logger.Error(MonLog);

            }
        }




        /// <summary>
        /// OnActionExecuted
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Controller.GetType().FullName == "Web.RestApi.FileUpLoaderController")
            {
                return;
            }

            MonLog.ExecuteEndTime = DateTime.Now;
            MonLog.ActionName = actionExecutedContext.ActionDescriptor.DisplayName;
            MonLog.ControllerName = actionExecutedContext.Controller.GetType().FullName;
            MonLog.ExecutedTime = (MonLog.ExecuteEndTime - MonLog.ExecuteStartTime).Milliseconds;
            _logger.Debug(MonLog);

            if (actionExecutedContext.Exception != null)
            {
                MonLog.Message = actionExecutedContext.Exception.Message;
                MonLog.StackTrace = actionExecutedContext.Exception.StackTrace;
                _logger.Error(MonLog);

            }
        }
    }
}
